package net.promethe.rsoul
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.collections.XMLListCollection;
	
	public class Configuration
	{
		private static const FILENAME	: String		= "configuration.xml";
		
		private static var _instance	: Configuration	= null;

		private var _contacts	: XMLListCollection	= null;		
		private var _data		: XML				= <rsoul-configuration>
														<login/>
														<password/>
														<location>Unkown Location</location>
														<autoConnect>false</autoConnect>
														<rememberPassword>false</rememberPassword>
														<state>actif</state>
														<contacts />
													  </rsoul-configuration>;
		
		public static function get configuration() : Configuration
		{
			if (!_instance)
				_instance = new Configuration();
			
			return (_instance);
		}
		
		[Bindable]
		public function set login(value : String) : void
		{
			_data.login = value;
		}
		
		public function get login() : String
		{
			return (_data.login);
		}
		
		[Bindable]
		public function set password(value : String) : void
		{
			_data.password = value;
		}
		
		public function get password() : String
		{
			return (_data.password);
		}
		
		[Bindable]
		public function set location(value : String) : void
		{
			_data.location = value;
		}
		
		public function get location() : String
		{
			return (_data.location);
		}
		
		public function get rememberPassword() : Boolean
		{
			return (_data.rememberPassword == "true");
		}
		
		[Bindable]
		public function set rememberPassword(value : Boolean) : void
		{
			_data.rememberPassword = value;
		}
		
		public function get autoConnect() : Boolean
		{
			return (_data.autoConnect == "true");
		}
		
		[Bindable]
		public function set autoConnect(value : Boolean) : void
		{
			_data.autoConnect = value;
		}
	
		public function Configuration()
		{
			load();
		}
		
		[Bindable]
		public function set contacts(value : XMLListCollection) : void
		{
			_contacts = value;
		}
		
		public function get contacts() : XMLListCollection
		{
			return (_contacts);
		}
		
		public function get state() : String
		{
			return (_data.state);
		}
		
		[Bindable]
		public function set state(value : String) : void
		{
			_data.state = value;
		}
	
		public function save() : void
		{
			var file 		: File			= File.applicationStorageDirectory.resolvePath(FILENAME);
			var stream		: FileStream 	= new FileStream();
			
			_data.replace("contacts", new XML("<contacts>" + _contacts.toXMLString() + "</contacts>"));
			
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(_data.toXMLString());
			stream.close();
		}
		
		public function load() : void
		{
			var file 	: File			= File.applicationStorageDirectory.resolvePath(FILENAME);
			var stream	: FileStream 	= new FileStream();
				
			if (file.exists)
			{
				stream.open(file, FileMode.READ);
				_data = new XML(stream.readUTFBytes(stream.bytesAvailable));
				_contacts = new XMLListCollection(_data.contacts.contact);
				stream.close();
			}
			else
			{
				_contacts = new XMLListCollection();
			}
		}
		
		public function removeContact(myContact : XML) : void
		{
			_contacts.removeItemAt(_contacts.getItemIndex(myContact));
		}

	}
}